package com.ficus.road.maintain.third.zh;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.RoadInfo;
import com.ficus.road.maintain.core.model.RoadTypeEnum;
import com.ficus.road.maintain.core.model.Unit;
import com.ficus.road.maintain.core.model.zh.RoadIntactStatistic;
import com.ficus.road.maintain.core.model.zh.RoadIntactZH;
import com.ficus.road.maintain.dao.RoadInfoDao;
import com.ficus.road.maintain.dao.RoadIntactDao;
import com.ficus.road.maintain.dao.UnitDao;
import com.ficus.road.maintain.util.FormatUtil;
import com.ficus.road.maintain.util.MyDateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/4/14 下午4:22
 */
@Component
public class ZHRoadIntact extends BaseZH {

    @Autowired
    private UnitDao unitDao;
    @Autowired
    private RoadInfoDao roadInfoDao;
    @Autowired
    private RoadIntactDao roadIntactDao;

    @PostConstruct
    void init() {
        arg0 = "sanwangSign";
        arg1 = "{\"pageNum\":7}";
        errMsg = "调用中海接口获取道路完好率异常!";
        successMsg = "调用中海接口获取道路完好率成功!";
    }

    /**
     * 前往中海获取道路完好率数据
     */
//        @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 14 * * ?", zone = "GMT+8:00")
//    @PostConstruct
    public void refreshRoadIntactInfo() {
        init();
        String roadIntactStr = null;
        int i = 0;
        while (i < 10) {
            roadIntactStr = getZHData();
            if (ObjectUtils.isNotEmpty(roadIntactStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(roadIntactStr)) {
            return;
        }
        Date today = new Date();
        String dateStr = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd");
        Map<String, String> unitIdNameMap = unitDao.findAll().stream().collect(Collectors.toMap(Unit::getId, Unit::getUnitName));
        Map<String, RoadInfo> roadIdInfoMap = roadInfoDao.findAll().stream().collect(Collectors.toMap(RoadInfo::getSecNum, item -> item, (entity1, entity2) -> entity1));
        List<RoadIntactZH> roadIntactZHList = JSONArray.parseArray(roadIntactStr, RoadIntactZH.class);
        RoadIntactZH roadIntactZH = roadIntactZHList.get(0);
        List<RoadIntactStatistic.RoadIntact> roadIntactList = roadIntactZH.getRoadData().parallelStream().map(item -> convert2RoadIntact(item, unitIdNameMap, roadIdInfoMap, dateStr, today)).collect(Collectors.toList());
        roadIntactList.forEach(item -> {
            roadIntactDao.saveOrUpdate(item);
        });
    }

    private RoadIntactStatistic.RoadIntact convert2RoadIntact(RoadIntactZH.RoadIntact zh, Map<String, String> unitIdNameMap, Map<String, RoadInfo> roadIdInfoMap, String dateStr, Date date) {
        RoadTypeEnum roadType = null;
        RoadInfo roadInfo = roadIdInfoMap.get(zh.getRoadid());
        if (ObjectUtils.isNotEmpty(roadInfo)) {
            if (ObjectUtils.isNotEmpty(roadInfo.getRoadType())) {
                roadType = roadInfo.getRoadType();
            } else if (ObjectUtils.isNotEmpty(roadInfo.getUnitId())) {
                if (roadInfo.getUnitId().contains("YHGL")) {
                    roadType = RoadTypeEnum.DIRECT;
                } else {
                    roadType = RoadTypeEnum.HIGHWAY;
                }
            } else {
                roadType = RoadTypeEnum.HIGHWAY;
            }
        } else {
            roadType = RoadTypeEnum.HIGHWAY;
        }
        RoadIntactStatistic.RoadIntact roadIntact = RoadIntactStatistic.RoadIntact.builder()
                .id(dateStr + "_" + zh.getRoadid())
                .roadId(zh.getRoadid())
                .roadName(roadIdInfoMap.getOrDefault(zh.getRoadid(), new RoadInfo()).getRoadName())
                .count(zh.getCount())
                .rate(FormatUtil.keepDecimalsOfK(zh.getRate(), 2))
                .unitName(unitIdNameMap.get(zh.getManagesectionid()))
                .realWorkLength(FormatUtil.keepDecimalsOfK(zh.getRealworklength(), 2))
                .width(FormatUtil.keepDecimalsOfK(zh.getWidth(), 2))
                .roadType(roadType)
                .date(dateStr)
                .modificationTime(date)
                .build();
        return roadIntact;
//        AuthenticationProvider
//        ProviderManager
//        AuthenticationManager
//        UsernamePasswordAuthenticationFilter
//        AnonymousAuthenticationFilter
//        DelegatingFilterProxy
//        UserDetailsService
//        SecurityBuilder
//        ApplicationListener
    }
}
